package com.zhxl.server.controller;

import com.zhxl.server.utils.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "图片和视频上传")
@Controller
public class UploadController {

    @Autowired
    private OssUtils ossUtils;

    @RequestMapping("/upload")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "（参数）图片", paramType = "query", dataType = "file"),
    })
    public JSONObject upload(HttpServletRequest httpServletRequest, @RequestParam("file") MultipartFile file){
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        HashMap<Object, Object> map1 = new HashMap<>();
        Map<String,Object> map = new HashMap<>();
        String url = ossUtils.checkImage(file);
        String[] imgUrls = url.split("\\?");
        map.put("code",0);
        map.put("msg","SUCCESS");
        map.put("data",map1);
        map1.put("src",imgUrls[0]);
        return JSONObject.fromObject(map);
    }


}
